package baiqitun.dynamictable;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 代码生成器
 * @author kedaji
 */
public class DynamicTableCodeGenerator {
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        String prefix = "Sys%s";
        GlobalConfig gc = new GlobalConfig();
        // 生成的代码放在哪里
        gc.setOutputDir("D:/code/mybatis-plus/dynamic-table");
        gc.setAuthor("kedaji");
        gc.setOpen(false);
        // 用户自定义Id
        gc.setIdType(IdType.INPUT);
        gc.setFileOverride(true);
        gc.setMapperName(prefix+"Mapper")
                .setControllerName(prefix+"Controller")
                .setXmlName(prefix+"Mapper")
                .setServiceName(prefix+"Service")
                .setEntityName(prefix)
                .setServiceImplName(prefix+"ServiceImpl");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/dynamic-table?characterEncoding=UTF-8&useUnicode=true&useSSL=false");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("12345678");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("baiqitun.dynamictable");
        pc.setXml("mapper");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setSuperEntityClass("baiqitun.dynamictable.entity.BaseEntity");
        // 数据库表公共字段
        strategy.setSuperEntityColumns("id","create_by","create_time", "update_by", "update_time", "deleted");
        strategy.setInclude(
                "test_student");

        strategy.setTablePrefix("test_");
        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
